Ext.ns('Taxamatch');
Taxamatch.SearchForm = function(config){
	        
	var ranksStore = new Ext.data.Store({
			proxy: {
					type: 'ajax'
				,	url: 'getSourceMenu.json'
				,	reader: {
							type: 'json'
						,	root: 'data'
					}
			}
		,	model: 'SourceMenu'
		,	autoLoad : true
		,	autoDestroy : true
	});
	
	Ext.apply(this, config, { 
			bodyPadding: 15
		,	draggable: false
		,	items: [{
					xtype: 'fieldset'
				,	instructions:'<p>Select the "Source" that you would like to use to search against. The name either a Genus or "Genus Species" that you wish to check.</p><p><i>Note: Please use "Search" button above for app to work correctly.</i></p>'
				,	defaults: {
							required: true
						,	labelAlign: 'left'
						,	labelWidth: '25%'
					}
				,	items: [{
							xtype: 'selectfield'
						,	name: 'source'
						,	label: 'Select Source'
						,	valueField: 'value'
						,	displayField: 'text'
						,	store: ranksStore
					}, {
							xtype: 'textfield'
						,	name: 'name'
						, value:'Homo'
						,	label: 'Name'
						,	useClearIcon: true
						,	autoCapitalize: false
					}]	
			}]	
		,	dockedItems: [{
					xtype: 'toolbar'
				,	dock: 'top'
				,	title: 'Search'
				,	items: [{
							text: 'Taxamatch'
						,	ui: 'back'
						,	handler: function() {
								Ext.getCmp('mainPanel').setActiveItem(0);							
							}	
					}]	
			},{
					xtype: 'toolbar'
				,	dock: 'bottom'
				,	items: [{
							text: 'Reset'
						,	scope:this	
						,	handler: function() {
								this.reset();
							}
					},{
						xtype: 'spacer'
					},{
							text: 'Search'
						,	ui: 'confirm'
						,	scope:this
						,	handler:this.sendSearchRequest
					}]
			}]				
	});
	
	Taxamatch.SearchForm.superclass.constructor.call(this, config);

};

Ext.extend(Taxamatch.SearchForm , Ext.form.FormPanel, {

		sendSearchRequest:function(){
			var params = this.getValues();
			if(params.name == ''){
				Ext.Msg.alert("Attention", "Please enter Scientific Name.");
				return;
			}
			var str = params.name;	
			/**Left whiteSpaces*/
			var re = /\s*((\S+\s*)*)/;
			str = str.replace(re, "$1");
	
			/**Right WhiteSpaces*/
			re = /((\s*\S+)*)\s*/;
			str = str.replace(re, "$1");
			
			Ext.Ajax.request({
					url: '../api/taxamatch.php'
				,	params: {
							cmd: 'taxamatch'
						,	debug: 0
						,	layout: 'flat'
						,	search_mode:'rapid'
						,	str: str
						,	source:	params.source
					}
				,	scope: this
				,	success: function(responseObject){
						var data = Ext.decode(responseObject.responseText);	
						Ext.getCmp('searchResultPanel').store.loadData(data.data);
						Ext.getCmp('searchResultPanel').getDockedItems()[0].setTitle("Results: " + str);
						Ext.getCmp('mainPanel').setActiveItem(3);		
					}	
			});
		}

});